import Vue from 'vue'
import VueRouter from 'vue-router'
import login from '@/components/login'
import home from '@/components/home'
import welcome from '@/components/welcome'
import users from '@/components/user/users'
import rights from '@/components/power/rights'
import roles from '@/components/power/roles'
import categories from '@/components/goods/categories'
import params from '@/components/goods/params'
import goods from '@/components/goods/List'
import goodsAdd from '@/components/goods/add'
import orders from '@/components/order/order'
import report from '@/components/report/report'

Vue.use(VueRouter)

const routes = [
  {
    path:'/',
    redirect: '/login'
  },
  {
    path:"/login",
    component : login
  },
  {
    path: "/home",
    component: home,
    redirect: '/welcome',
    children:[
      {
        path:'/welcome',
        component:welcome
      },
      {
        path:'/users',
        component:users
      },
      {
        path:'/rights',
        component:rights
      },
      {
        path: '/roles',
        component: roles
      },
      {
        path:'/categories',
        component: categories
      },
      {
        path: '/params',
        component: params
      },
      {
        path: '/goods',
        component: goods
      },
      {
        path: '/goods/add',
        component: goodsAdd
      },
      {
        path: '/orders',
        component: orders
      },
      {
        path: '/reports',
        component: report
      }
    ]
  }
]

const router = new VueRouter({
  routes
})

router.beforeEach((to,form,next)=>{
  if(to.path == "/login") return next()

  const tokenStr = window.sessionStorage.getItem('token')

  if(! tokenStr) return next('/login')

  next()


})


export default router
